package com.bw.user.consumer;

import com.bw.user.util.MsgUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName UserConsumer
 * @Author Hao_YunKai
 * @Date 2022/3/25 20:11
 */
@Component
public class UserConsumer {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RabbitListener(queues = {"codeQueue"})
    public void takeCode(Channel channel, Message message, String phone) throws IOException {
        String code = MsgUtil.sendMsg(phone);
        redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
        System.out.println("Consumer:{\"code\":" + code + "}");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
